package com.test.daily.leetcode.y2022.m07.day0705.v01;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/5 上午 10:59
 * @version: 1.0
 */
public class Solution {
    public static class DoubleEndNode{
        public DoubleEndNode last;
        public DoubleEndNode next;
        public int value;
        public DoubleEndNode(int value){
            this.value = value;
        }
    }
    public static class DoubleEndNodeImplementQueueAndStack{
        public DoubleEndNode head;
        public DoubleEndNode tail;
        public  void addFromHead(int value){
            DoubleEndNode newNode = new DoubleEndNode(value);
            if(head == null){
               head = newNode;
               tail = newNode;
            } else {
                newNode.next = head;
                head.last = newNode;
                head = newNode;
            }
        }
        public void addFromTail(int value){
            DoubleEndNode newNode = new DoubleEndNode(value);
            if(head == null){
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                newNode.last = tail;
                tail = newNode;
            }
        }
        public DoubleEndNode deleteFromHead(){
            if(head == null){
                return null;
            }
            if(head == tail){
                DoubleEndNode node = head;
                head = null;
                tail = null;
                return node;
            }
            DoubleEndNode node = head;
            head = head.next;
            node.next = null;
            head.last = null;
            return node;
        }

        public DoubleEndNode deleteFromTail(){
            if(tail == null){
                return null;
            }
            if(head == tail){
                DoubleEndNode node = head;
                head = null;
                tail = null;
                return node;
            }
            DoubleEndNode node = tail;
            tail = tail.last;
            tail.next = null;
            node.last = null;
            return node;
        }
    }
    public static class MyQueue{
        public DoubleEndNodeImplementQueueAndStack myQueuq;
        public MyQueue(){
            this.myQueuq = new DoubleEndNodeImplementQueueAndStack();
        }
        public void offer(int value){
            this.myQueuq.addFromTail(value);
        }
        public int pop(){
            return this.myQueuq.deleteFromHead().value;
        }
    }
}
